<?php
class HomeController extends Apps_Controller_Action_Default {
	public function preDispatch() {
		$layout = new Zend_Layout();
		$layout->banner = 'banner';
	}

	public function indexAction() { 
	    $tourCateModel     = new M_TourCategory();
        $tourModel         = new M_Tour();
        $cruiseCateModel   = new M_CruiseCategory();
        $cruiseModel       = new M_Cruise();
        $tourStyle         = new M_TourStyle();
        $countryModel      = new M_Country();
        $regionModel       = new M_Region();
        $articleModel      = new M_Article();
        $areaModel    	   = new M_Area();

	    //list tour
	    $listTourCate = $tourCateModel->listCategoryShowHome()->query()->fetchAll();
	    if($listTourCate) {
	        foreach($listTourCate as $k => $v) {
                $listTourCate[$k]['list_tour'] = $tourModel->listTourByCategoryId(intval($v['id']))->query()->fetchAll() ;
	        }
	    }

	    //list cruise
        $listCruiseCate = $cruiseCateModel->listCategoryShowHome()->query()->fetchAll();
        $this->view->cruiseCategoryTitle = $listCruiseCate[0]['cruise_category_title'];
        
        if($listCruiseCate) {
        	
            $listCruiseCate = $this->listCruise($listCruiseCate[0]['id']);
            $this->view->listCruiseCate  = $listCruiseCate;
        }
        
        //Trip in style : get trip style of Vietnam ; counttry_id = 1
        $listTourStyle = $tourStyle->listStyleShowHome()->query()->fetchAll();

        //Hotel and resort: by location, coutry
        $listCountry = $countryModel->listCountry()->query()->fetchAll();
        if($listCountry) {
            foreach($listCountry as $k => $v) {
		        if($v['id'] == 1) { //Check is VIETNAM
		        	$area = $areaModel->listareaByCountryId($v['id'])->query()->fetchAll();
		        	if($area) {
			        	foreach($area as $key => $value) {
			        		$region = $regionModel->listRegionHotelByAreaId($value['id'])->query()->fetchAll();
			        		$area[$key]['list_region'] = $region;
			        	}
			        	$listCountry[$k]['area'] = $area;
		        	}
		        } else {
	                $listCountry[$k]['list_region'] = $regionModel->listRegionByCountryId($v['id'])->query()->fetchAll();
	                $listCountry[$k]['area'] = NULL;
		        }
            }
        }
        
        //coming events
        $eventModel = new M_Event();
        $listComingEvent = $eventModel->listComingEvent()->query()->fetchAll();

        $this->view->listComingEvent = $listComingEvent;
        $this->view->listCountry     = $listCountry;
        $this->view->listTourStyle   = $listTourStyle;
        $this->view->listTourCate    = $listTourCate;
	}
	
	
	//GET CRUISE
	public function listCruise($categoryId) {
		if(!$categoryId) {
			return null;
		}
		$cruiseModel     	 = new M_Cruise();
		$cruiseTypeModel 	 = new M_CruiseType();
		$cruiseItineraryTourModel = new M_CruiseItineraryTour();
	
		$cruiseType = $cruiseTypeModel->getList()->query()->fetchAll();
		if($cruiseType) {
			foreach($cruiseType as $k => $v) {
				$listParentCruise = $cruiseModel->listCruiseMenuLeft($categoryId, $v['id'])->query()->fetchAll();
				foreach($listParentCruise as $key => $value) {
					$listCruise = $cruiseModel->listCruiseMenuLeft($categoryId, $v['id'], $value['id'])->query()->fetchAll();
	
					if($listCruise) {
						foreach($listCruise as $cK => $cruise) {
							$cruiseItineraryTour = $cruiseItineraryTourModel->getCruiseItineraryTourByCruiseId($cruise['id'])->query()->fetch();
							if($cruiseItineraryTour) {
								$listCruise[$cK]['link_id']    = $cruiseItineraryTour['id'];
								$listCruise[$cK]['link_title'] = $cruiseItineraryTour['title'];
							} else {
								$listCruise[$cK]['link_id']    = -1;
								$listCruise[$cK]['link_title'] = $cruise['name'];
							}
						}
						$listParentCruise[$key]['link_id']    = -1;
						$listParentCruise[$key]['link_title'] = $cruise['name'];
						$listParentCruise[$key]['list_child_cruise'] = $listCruise;
					} else {
						$cruiseItineraryTour = $cruiseItineraryTourModel->getCruiseItineraryTourByCruiseId($value['id'])->query()->fetch();
            			if($cruiseItineraryTour) {
	            			$listParentCruise[$key]['link_id']    = $cruiseItineraryTour['id'];
	            			$listParentCruise[$key]['link_title'] = $cruiseItineraryTour['title'];
            			} else {
	            			$listParentCruise[$key]['link_id']    = -1;
	            			$listParentCruise[$key]['link_title'] = $value['name'];
            			}
					}
				}
				$cruiseType[$k]['list_cruise'] = $listParentCruise;
			}
			return $cruiseType;
		}
		return null;
	}
}